import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path";
import path from "path";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import { svgBuilder } from "./src/plugins/svgBuilder.js";

export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd(), "");

  const { VITE_BASE_API, VITE_BASE_URL } = env;
  return {
    base: "./",
    resolve: {
      alias: {
        "@": resolve(__dirname, "src"),
      },
    },
    plugins: [
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
      svgBuilder("./src/assets/icons/svg/"),
    ],
    server: {
      host: "0.0.0.0",
      cors: true,
      open: true,
      proxy: {
        [VITE_BASE_API]: {
          target: VITE_BASE_URL,
          // secure: false,
          changeOrigin: true,
          rewrite: (path) => path.replace(["^" + VITE_BASE_API], ""),
        },
      },
    },
  };
});
